package util;

import java.awt.Color;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;

public class Constants {

  public static final String SystemIDFile = "resources/Sovereignty.xml";
  public static final String ItemIDFile   = "resources/typeid.txt";

  public static enum EVEData {
    Users, Systems, Items, Users_Items, Users_Systems, Configuration;
  }

  public static final String _findNameToID(String lookupID, String file) throws IOException {
    BufferedReader fileIDReader = new BufferedReader(new InputStreamReader(new FileInputStream(file)));
    String id = "";
    String name = "";
    String[] splittedLine = null;

    while (!id.equals(lookupID)) {
      String line = fileIDReader.readLine();
      if (line != null) {
        splittedLine = line.split(" | ");
        id = splittedLine[0];
      }
      else {
        id = "unknown";
        name = "unknown";
        break;
      }
    }

    if (!id.equals("unknown")) {
      name = splittedLine[2];
      for (int i = 3; i < splittedLine.length; i++) {
        name += " " + splittedLine[i];
      }
    }

    fileIDReader.close();
    return name;
  }

  public static final String _addDotsToNumber(String number) {
    String result = number;
    if (number.contains(".")) {
      int position = (number.indexOf(".") == -1 ? number.length() : number.indexOf("."));
      String head = number.substring(0, position);
      for (int i = head.length() - 3; i > 0; i = i - 3) {
        String IThead = result.substring(0, i);
        String ITtail = result.substring(i, result.length());
        result = IThead + "." + ITtail;
      }
    }
    return result;
  }

  public static final double _dotNumberToDouble(String number) {
    String result = "";
    if (number.contains(".")) {
      String[] numbers = number.split("\\.");
      for (int i = 0; i < numbers.length - 1; i++) {
        result += numbers[i];
      }
      result += ".";
      result += numbers[numbers.length - 1];
    }

    if (number.equals("unknown")) return -1.0;
    return Double.valueOf(result);
  }

  public static final String _eveDataToString(EVEData data) {
    String result = "";
    if (data.equals(Constants.EVEData.Users)) result = "users";
    if (data.equals(Constants.EVEData.Items)) result = "items";
    if (data.equals(Constants.EVEData.Systems)) result = "systems";
    if (data.equals(Constants.EVEData.Users_Items)) result = "users_items";
    if (data.equals(Constants.EVEData.Users_Systems)) result = "users_systems";
    if (data.equals(Constants.EVEData.Configuration)) result = "configuration";
    return result;
  }

  public static final Color _getPercentageGradient(int percent) {
    Color result = null;
    if (percent < 5) result = new Color(255, 255, 204);
    if (percent >= 5 & percent < 10) result = new Color(255, 255, 153);
    if (percent >= 10 & percent < 15) result = new Color(255, 230, 204);
    if (percent >= 15 & percent < 20) result = new Color(255, 204, 255);
    if (percent >= 20 & percent < 25) result = new Color(255, 153, 255);
    if (percent >= 25 & percent < 30) result = new Color(255, 102, 255);
    if (percent >= 30 & percent < 40) result = new Color(255, 51, 255);
    if (percent >= 40 & percent < 50) result = new Color(255, 0, 255);
    if (percent >= 50 & percent < 60) result = new Color(255, 0, 204);
    if (percent >= 60 & percent < 70) result = new Color(255, 0, 153);
    if (percent >= 70 & percent < 80) result = new Color(255, 0, 102);
    if (percent >= 80 & percent < 90) result = new Color(255, 0, 51);
    if (percent >= 90) result = new Color(255, 0, 0);
    return result;
  }
}
